//
//  RZTextMessageCell.m
//  RZIM
//
//  Created by reyzhang on 2021/8/19.
//

#import "RZTextMessageCell.h"
#import "RZMessageCellTool.h"
#import "RZIMConfig.h"
#import "RZKitUtility.h"
#import "RZKitCommonDefine.h"


#define TEXT_SPACE_LEFT 12
#define TEXT_SPACE_RIGHT 12
#define TEXT_SPACE_TOP 9.5
#define TEXT_SPACE_BOTTOM 9.5
#define DESTRUCT_TEXT_ICON_WIDTH 13
#define DESTRUCT_TEXT_ICON_HEIGHT 28



@implementation RZTextMessageCell

#pragma mark - Life Cycle

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self initialize];
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self initialize];
    }
    return self;
}



- (void)initialize {
    
    [self showBubbleBackgroundView:YES];

    [self.messageContentView addSubview:self.textLabel];
}



+ (CGSize)sizeForMessageModel:(RZMessageInfo *)model
      withCollectionViewWidth:(CGFloat)collectionViewWidth
         referenceExtraHeight:(CGFloat)extraHeight {
    CGFloat messagecontentview_height = [self getMessageContentHeight:model];
    messagecontentview_height += extraHeight;
    return CGSizeMake(collectionViewWidth, messagecontentview_height);
}


- (void)setDataModel:(RZMessageInfo *)model {
    [super setDataModel:model];
    [self setAutoLayout];
}


- (void)setAutoLayout {
    NSString *textMessage = self.model.msg;
    
    self.textLabel.text = textMessage;
    
    CGSize labelSize = [[self class] getTextSize:self.model];//textlabelsize
    
    float maxWidth = [RZMessageCellTool getMessageContentViewMaxWidth];
    CGFloat bubbleHeight = [[self class] getMessageContentHeight:self.model];
    CGFloat bubbleWidth = labelSize.width + TEXT_SPACE_RIGHT + TEXT_SPACE_LEFT;
    if (bubbleWidth >= maxWidth) {
        bubbleWidth = maxWidth;
    }
    
//    [self setCSEvaUILayout:bubbleWidth bubbleHeight:bubbleHeight];

    self.messageContentView.contentSize = CGSizeMake(bubbleWidth, bubbleHeight);
    
    if (self.model.messageDirection == MessageDirection_RECEIVE) {
        [self.textLabel setTextColor: RGBCOLOR(51, 51, 51)];
        self.textLabel.frame =  CGRectMake(TEXT_SPACE_LEFT, (bubbleHeight - labelSize.height) / 2, labelSize.width, labelSize.height);
    } else {
        [self.textLabel setTextColor:RGBCOLOR(51, 51, 51)];
        self.textLabel.frame =  CGRectMake(TEXT_SPACE_LEFT, (bubbleHeight - labelSize.height) / 2, labelSize.width, labelSize.height);
    }
    
}

+ (CGFloat)getMessageContentHeight:(RZMessageInfo *)model{
    CGSize textMessageSize = [self getTextSize:model];
    //背景图的最小高度
    CGFloat messagecontentview_height = textMessageSize.height + TEXT_SPACE_TOP + TEXT_SPACE_BOTTOM;

    if (messagecontentview_height < RZIMConfigCenter.ui.globalMessagePortraitSize.height) {
        messagecontentview_height = RZIMConfigCenter.ui.globalMessagePortraitSize.height;
    }
    return messagecontentview_height;
}

+ (CGSize)getTextSize:(RZMessageInfo *)model{
    CGFloat textMaxWidth = [RZMessageCellTool getMessageContentViewMaxWidth] - TEXT_SPACE_LEFT - TEXT_SPACE_RIGHT;
    NSString *textMessage = model.msg;
    CGSize textMessageSize;
    
    textMessageSize = [RZKitUtility getTextDrawingSize:textMessage
                                                   font:[RZIMConfigCenter.font fontOfSecondLevel]
                                        constrainedSize:CGSizeMake(textMaxWidth, 80000)];
    if (textMessageSize.width > textMaxWidth) {
        textMessageSize.width = textMaxWidth;
    }
    textMessageSize = CGSizeMake(ceilf(textMessageSize.width), ceilf(textMessageSize.height));
    return textMessageSize;
}



#pragma mark - Getter & Setter
- (UILabel *)textLabel{
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [_textLabel setFont:[RZIMConfigCenter.font fontOfSecondLevel]];
        _textLabel.numberOfLines = 0;
        [_textLabel setLineBreakMode:NSLineBreakByWordWrapping];
        [_textLabel setTextAlignment:NSTextAlignmentLeft];
    }
    return _textLabel;
}
@end
